package me.zhengjie.modules.rest.enterprise;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.modules.domain.enterprise.EnterpriseInformationProduct;
import me.zhengjie.modules.service.EnterpriseInfProService;
import me.zhengjie.modules.utils.Contents;
import me.zhengjie.modules.utils.JsonUtils;
import me.zhengjie.modules.utils.ReturnObject;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("企业产品信息api")
@RestController
@RequiredArgsConstructor
public class EnterpriseInfProController {

    private final EnterpriseInfProService enterpriseInfProService;

    @PostMapping("/EnterInfPro/addEnterpriseInfProByList")
    public @ResponseBody Object addEnterpriseInfProByList(@RequestBody String json){
        List<EnterpriseInformationProduct> productList= JsonUtils.JsonToEnterpriseInfProList(json);
        ReturnObject returnObject=new ReturnObject();
        try {
            int num=enterpriseInfProService.addEnterpriseInfProByList(productList);
            if(num>0){
                returnObject.setCode(Contents.RETURN_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contents.RETURN_CODE_FAIL);
                returnObject.setMessage(Contents.RETURN_MESSAGE_FAIL);
            }
        }catch (Exception e){
            e.printStackTrace();
            returnObject.setCode(Contents.RETURN_CODE_FAIL);
            returnObject.setMessage(Contents.RETURN_MESSAGE_EXCEPTION);
        }
        return returnObject;
    }

    @ApiOperation("根据企业名称分页查询企业产品信息")
    @AnonymousAccess
    @GetMapping("/queryEnterpriseInfProForPageByName")
    public Map<String,Object> queryEnterpriseInfProForPageByName(String name,@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("pageNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);
        List<EnterpriseInformationProduct> list=enterpriseInfProService.queryEnterpriseInfProForPageByName(map);
        int total=enterpriseInfProService.queryEnterpriseInfProForTotalByName(map);
        map=new HashMap<>();
        map.put("list",list);
        map.put("total",total);
        return map;
    }
}
